﻿#region references
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

/* Project dependencies */
using IcaGames.Objects;
using IcaGames.Database.ObjectManagement;
#endregion

namespace IcaGames.Security
{
    /*
     * Class that handles the Membership login for all users
     * 
     * @author
     * Rick Oonk
     * 
     * @version
     * 1.0
     * 
     * @update
     * 01-12-2009 by Rick Oonk.
     * Created Class and added attributes and standard functions. 
     */ 
    public class CustomMembershipProvider : MembershipProvider
    {
        #region not used functions
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region override functions
        /*
         * Function to validate a user given a specified email and password.
         * 
         * @param
         * username: The email of the user.
         * password: The password of the user.
         * 
         * @returns
         * True if the user was succesfully validated.
         * False if the user was not succesfully validated.
         */ 
        public override bool ValidateUser(string username, string password)
        {
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
            if (!UserManagement.ValidateUser(username, password)) { return false; }
            
            return true;
        }

        /*
         * Function that checks if this option is enabled for the CreateUserWizard Control.
         */ 
        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }
        #endregion

        #region new functions
        /*
         * Function to retrieve the userId of a user with a specified e-mailaddress.
         * 
         * @param
         * email: The e-mailaddress of the user.
         * 
         * @returns
         * The userId of the user.
         */ 
        public int GetUserId(string email)
        {
            return UserManagement.GetUserId(email);
        }

        /*
         * Function to retrieve a MembershipUser from the database.
         * 
         * @param
         * userId: The userId of the user that has to be retrieved from the database.
         */ 
        public User GetUser(int userId)
        {
            User user = new User();
            if (!UserManagement.GetUser(userId, user)) { return null; }

            return user;
        }

        /*
         * Function to register a new user and put the information into the database.
         * 
         * @param
         * email: The e-mailadress of the new user.
         * password: The password of the new user.
         * firstName: The firstname of the new user.
         * lastName: The lastname of the new user.
         * 
         * @returns
         * The new inserted user.
         */ 
        public User CreateUser(string email, string password, 
            string firstName, string lastName, string prefix)
        {
            User user = new User
            {
                Email = email,
                Password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5"),
                FirstName = firstName,
                LastName = lastName,
                Prefix = prefix,
            };
            if (!UserManagement.AddUser(user)) { return null; }

            return user;
        }
        #endregion
    }
}
